#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 6e5 + 10, M = N * 2;

int n, root;
int h[N], e[M], ne[M], w[M], idx;
LL d[N], res;

void add(int a, int b, int c){
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

void dfs(int u, int father){
    for(int i = h[u]; ~i; i = ne[i]){
        int j = e[i];
        if(j != father){
            dfs(j, u);
            d[u] = max(d[u], d[j] + w[i]);
        }
    }


    for(int i = h[u]; ~i; i = ne[i]){
        int j = e[i];
        if(j != father){
            res += d[u] - d[j] - w[i];
        }
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    memset(h, -1, sizeof h);

    cin >> n >> root;
    for(int i = 1; i < n; i ++){
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }

    dfs(root, -1);
    cout << res << endl;
    return 0;
}